﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProBugTracker.Core.Interface;
using ProBugTracker.Data.Interface;
using ProBugTracker.Data;
using ProBugTracker.Domain;

namespace ProBugTracker.Core
{
    public class RoleManager : IRoleManager
    {
        private IRoleManagerData _roleManagerData;
        private IPermissionManagerData _permissionManagerData;


        public RoleManager(IRoleManagerData roleManagerData, IPermissionManagerData permissionManagerData)
        {
            this._roleManagerData = roleManagerData;
            this._permissionManagerData = permissionManagerData;
        }



        public bool AddNewRole(string roleName)
        {
            try
            {
                UserRoleDB userRoleDB = new UserRoleDB()
                {
                    RoleName = roleName
                };

                return _roleManagerData.AddNewRole(userRoleDB);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool EditRoleName(int roleID, string roleName)
        {
            try
            {
                UserRoleDB userRoleDB = new UserRoleDB()
                {
                    ID=roleID,
                    RoleName = roleName
                };

                return _roleManagerData.EditRoleName(roleID,userRoleDB);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool RemoveRole(int roleID)
        {
            try
            {
                return _roleManagerData.RemoveRole(roleID);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public IList<UserAssignRole> FindUserAssignRoleByUserID(int roleID)
        {
            try
            {
                IList<UserAssignRoleDB> userAssignRoleDBList = _roleManagerData.FindUserAssignRoleByUserID(roleID);
                IList<UserAssignRole> userAssignRoleList = new List<UserAssignRole>();
                foreach (UserAssignRoleDB userAssignRoleDB in userAssignRoleDBList)
                {
                    string roleName = _roleManagerData.FindUserRoleByID(roleID).RoleName;
                    IList<RoleAssignPermissionDB> roleAssignPermissionDBList = _permissionManagerData.ReadRolePermissionByRoleID(roleID);
                    IList<RoleAssignPermission> permissionLevelList = new List<RoleAssignPermission>();
                    foreach (RoleAssignPermissionDB roleAssignPermissionDB in roleAssignPermissionDBList)
                    {
                        string permissionName = _permissionManagerData.ReadPermissionByPermissionID(roleAssignPermissionDB.UserPermissionID).PermissionName;
                        RoleAssignPermission roleAssignPermission = new RoleAssignPermission()
                        {
                            UserRoleID = roleID,
                            UserRolePermissionID = roleAssignPermissionDB.UserPermissionID,
                            PermissionName = permissionName,
                            PermissionStartDate = roleAssignPermissionDB.PermissionStartDate,
                            PermissionEndDate = roleAssignPermissionDB.PermissionEndDate,
                            Active = roleAssignPermissionDB.Active
                        };
                    }
                    UserAssignRole userAssignRole = new UserAssignRole()
                    {
                        UserRoleID = userAssignRoleDB.UserRoleID,
                        RoleName = roleName,
                        EndDate = (DateTime)userAssignRoleDB.EndDate,
                        Active = userAssignRoleDB.Active
                    };

                    userAssignRoleList.Add(userAssignRole);
                }

                return userAssignRoleList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public bool AddRolePermission(int roleID, int permissionID, DateTime permissionStartDate, DateTime permissionEndDate)
        {
            throw new NotImplementedException();
        }

    }
}
